# --------------------------------------------------------
# ' BG project 
# ' this file combines preprocessed CNES 1992, ANES 2000, ANES 2008, TESS 2016 file 
# ' then merge with close election state indicators across four elections 
# --------------------------------------------------------
library(rio)
library(data.table)
library(maps)
library(stringr)
library(ggplot2)
library(ggsci)
library(ggpubr)

# specify your root directory here
here = '~/Dropbox/project-archive/BG-bk/dataverse'

source(file.path(here,'code','rdata_utils.R'))

# --------------------------------------------------------
# read data 
anes2000 <- as.data.table(import(file.path(here,"data","processed","anes_2000.dta")))
anes2008 <- as.data.table(import(file.path(here,"data","processed","anes_2008.dta")))
tess2016 <- as.data.table(import(file.path(here,"data","processed","tess_2016.dta")))

anes2000[, isolation := ifelse(n_size == 0, "isolated", "not isolated")]
tab2000 = anes2000[!is.na(isolation) & !is.na(r_talk_freq), .(N=.N), by=c('isolation','r_talk_freq')]
tab2000[, prop := N/sum(N)*100, by='isolation']

anes2008[, isolation := ifelse(n_size == 0, "isolated", "not isolated")]
tab2008 = anes2008[!is.na(isolation) & !is.na(r_talk_freq), .(N=.N), by=c('isolation','r_talk_freq')]
tab2008[, prop := N/sum(N)*100, by='isolation']

tess2016[, isolation := ifelse(n_size == 0, "isolated", "not isolated")]
tab2016 = tess2016[!is.na(isolation) & !is.na(wt) & k_imp == 0, .(N=.N), by=c('isolation','r_pol_talk')]
tab2016[, prop := N/sum(N)*100, by='isolation']
tab2016 = rbind(tab2016, data.table(isolation='isolated',r_pol_talk=4,N=0,prop=0))
tab2016[, r_pol_talk := factor(r_pol_talk, levels=c(1,2,3,4), labels=c('Never','Rarely','Sometimes','Often'))]

Q_2000 = "How many days in the past week did you talk \nabout politics with family or friends?"
Q_2008 = "During a typical week, how many days do you \ntalk about politics with family or friends?"
Q_2016 = "When you get together with your friends, \nrelatives or fellow workers, \nhow often do you discuss politics?"

p = ggplot(tab2000, aes(x=r_talk_freq,y=prop,fill=isolation,color=isolation)) 
p = p + geom_bar(stat='identity', position=position_dodge()) + theme_pubr() 
p = p + geom_text(aes(label=round(prop,0)), color="white",
    position = position_dodge(1), size=3.5, hjust=0.5, vjust=1.5)
p = p + scale_fill_nejm(name='')+scale_color_nejm(name='') 
p = p + scale_x_continuous(breaks=0:7)
p2000 = p + labs(x='Days in the past week',y='%',title='',subtitle=Q_2000) 

p = ggplot(tab2008, aes(x=r_talk_freq,y=prop,fill=isolation,color=isolation)) 
p = p + geom_bar(stat='identity', position=position_dodge()) + theme_pubr() 
p = p + geom_text(aes(label=round(prop,0)), color="white",
    position = position_dodge(1), size=3.5, hjust=0.5, vjust=1.5)
p = p + scale_fill_nejm(name='')+scale_color_nejm(name='') 
p = p + scale_x_continuous(breaks=0:7)
p2008 = p + labs(x='Days in the past week',y='%',title='',subtitle=Q_2008) 

p = ggplot(tab2016, aes(x=r_pol_talk,y=prop,fill=isolation,color=isolation)) 
p = p + geom_bar(stat='identity', position=position_dodge()) + theme_pubr() 
p = p + geom_text(aes(label=round(prop,0)), color="white",
    position = position_dodge(1), size=3.5, hjust=0.5, vjust=1.5)
p = p + scale_fill_nejm(name='')+scale_color_nejm(name='') 
p2016 = p + labs(x='Frequency',y='%',title='',subtitle=Q_2016) 

p = ggarrange(p2000, p2008, p2016, ncol=3, labels=c('ANES 2000','ANES 2008','TESS 2016'))
ggsave(file.path(here,'output','figure_s3_isolation.png'), p, width=13, height=5, dpi=600)

